package com.ssu.judea.travlog;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.ssu.judea.travlog.util.JSONParseHelper;
import com.ssu.judea.travlog.util.UrlImageConverter;
import com.ssu.judea.travlog.view.ResultPointItemizedOverlay;
import com.ssu.judea.travlog.vo.ResultPost;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

public class ViewRecentActivity extends TravlogActivity {
	
	private MapView mapView;
	private Handler mHandler;
	private getRecentopenPostsRunnable getRecentopenPostsRunnable;
	private ResultPointItemizedOverlay itemizedOverlay;
	private ArrayList<ResultPost> resultList;
	private int cursorCount = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.viewrecent);
		mapView = (MapView)findViewById(R.id.mapviews);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		mapView.setClickable(true);
		mapView.setBuiltInZoomControls(false);
		mapView.getController().setZoom(mapView.getMaxZoomLevel()-4);
		
		getRecentOpenPostsThread();
		
	}
	
	private void getRecentOpenPostsThread(){
		if(mHandler==null)
			mHandler = new Handler();
		getRecentopenPostsRunnable = new getRecentopenPostsRunnable();
		if(cursorCount==0){
			mHandler.postDelayed(getRecentopenPostsRunnable, 500);
		}else{
			mHandler.postDelayed(getRecentopenPostsRunnable, 6000);
		}
		
	}
	
	private class getRecentopenPostsRunnable implements Runnable {
		public void run() {
			if(resultList==null || cursorCount==0)
				getRecentOpenPosts();
			else{
				if(cursorCount<resultList.size()){
					mapView.getController().animateTo(new GeoPoint((int)(resultList.get(cursorCount).getLatitude()*1E6), (int)(resultList.get(cursorCount).getLongitude()*1E6)));
					getTravlogApp().alert(resultList.get(cursorCount).getTwitterid());
					cursorCount++;
				}
				else{
					cursorCount = 0;
				}
				getRecentOpenPostsThread();
			}
			
		}
	}{
		
	}
	@Override
	protected void onPause() {
		super.onPause();
		mHandler.removeCallbacks(getRecentopenPostsRunnable);
		getRecentopenPostsRunnable = null;
		cursorCount = 0;
	}
	
	private void getRecentOpenPosts(){
		if(ViewRecentActivity.this!=null){
			getTravlogApp().getRecentOpenPosts(ViewRecentActivity.this);
		}
	}
	
	public void completeRecentOpenPosts(String result){
		try {
			resultList = JSONParseHelper.parseToResultRecentPostList(new JSONArray(result));
			mapView.getOverlays().remove(itemizedOverlay);
				
			itemizedOverlay = new ResultPointItemizedOverlay(this,this
				.getResources().getDrawable(R.drawable.marker),resultList);
			for(ResultPost cur : resultList){
				UrlImageConverter converter = new UrlImageConverter();
				itemizedOverlay.addOverlay(new OverlayItem(new GeoPoint((int)(cur.getLatitude()*1E6), (int)(cur.getLongitude()*1E6)), cur.getMemberimageurl(), ""), 
						converter.urlToDrawable(cur.getMemberimageurl()));
			}
			
			mapView.getOverlays().add(itemizedOverlay);
			
			mapView.invalidate();
			cursorCount = 0;
			if(resultList.size()>0 && resultList.get(cursorCount)!=null){
				mapView.getController().animateTo(new GeoPoint((int)(resultList.get(cursorCount).getLatitude()*1E6), (int)(resultList.get(cursorCount).getLongitude()*1E6)));
				getTravlogApp().alert(resultList.get(cursorCount).getTwitterid());
				cursorCount++;
				getRecentOpenPostsThread();
			}
		} catch (JSONException e) {
			Log.e("changdoc", e.getMessage());
		} 
		
	}


	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
}
